import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:pdf_render/pdf_render_widgets.dart';

import '../../base/MyScaffold.dart';
import '../../model/PDFFile.dart';
import '../../res.dart';
import '../../sqflite/CreateTableSqls.dart';
import '../../sqflite/DBUtil.dart';
import '../../utils/color/ColorUtils.dart';
import '../../widgets/MyLocalImage.dart';
import '../../widgets/TextView.dart';
import '../convert/PDFView.dart';
import '../convert/Selected.dart';
import '../dialog/SetDialog.dart';

class SearchList extends StatefulWidget {
  SearchList({Key? key, required this.search}) : super(key: key);
  String search;

  @override
  State<SearchList> createState() => _SearchListState();
}

class _SearchListState extends State<SearchList> {
  TextEditingController controller = TextEditingController();
  String search = "";
  List<PDFFile> list = [];
  DBUtil? dbUtil;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    dbUtil = DBUtil();
    controller.value = TextEditingValue(text: widget.search);
    getData(widget.search);
  }

  getData(String search) async {
    list.clear();
    await dbUtil!.open();
    String sql = 'SELECT * FROM file WHERE name LIKE ?';
    await dbUtil!.queryList(sql, ['%' + search + '%']).then((value) {
      for (Map<String, dynamic> map in value) {
        bool star = map["star"] == "false" ? false : true;
        bool selects = map["selects"] == "false" ? false : true;
        list.add(PDFFile(
            id: map["id"] as int,
            name: map["name"],
            path: map["path"],
            time: map["time"],
            star: star,
            select: selects,
            size: map["size"]));
      }
      print(value);
      setState(() {});
    });
  }

  void submit(value) async {}

  @override
  Widget build(BuildContext context) {
    return MyScaffold(
      navBarHide: true,
      body: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: (){
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Container(
          margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 20),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.max,
            children: [
              Row(
                children: [
                  Expanded(
                    child: Container(
                      height: 44,
                      padding: const EdgeInsets.only(left: 10),
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(6),
                          color: rgba(247, 248, 250, 1)),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          const MyLocalImage(
                            Res.nav_icon_search,
                            width: 20,
                            height: 20,
                          ),
                          const SizedBox(
                            width: 10,
                          ),
                          Expanded(
                            child: TextField(
                              controller: controller,
                              onChanged: (v) {
                                setState(() {
                                  search = v;
                                });
                              },
                              onSubmitted: (v) {
                                submit(v);
                              },
                              textInputAction: TextInputAction.search,
                              decoration: InputDecoration(
                                suffixIcon: Container(
                                  padding: const EdgeInsets.symmetric(
                                      vertical: 14, horizontal: 16),
                                  child: Offstage(
                                    offstage: search.isEmpty ? true : false,
                                    child: InkWell(
                                      onTap: () {
                                        controller.clear();
                                        setState(() {
                                          search = "";
                                        });
                                      },
                                      child: Container(
                                        child: const MyLocalImage(
                                          Res.sys_icon_delete_grey3x,
                                          width: 10,
                                          height: 10,
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                                border: InputBorder.none,
                                hintText: 'search files',
                                hintStyle:
                                    TextStyle(color: rgba(200, 202, 204, 1)),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  TextView(
                    "Cancle",
                    padding: const EdgeInsets.only(left: 20),
                    style: TextStyle(color: ColorUtils.text),
                    onclick: () {
                      Get.back();
                    },
                  ),
                ],
              ),
              Expanded(
                child: list!.isEmpty
                    ? Container(
                        alignment: Alignment.center,
                        padding: const EdgeInsets.only(top: 100),
                        child: Column(
                          children: [
                            const MyLocalImage(
                              Res.empty_img_record,
                              width: 100,
                              height: 100,
                            ),
                            TextView(
                              "No search results yet",
                              textAlign: TextAlign.center,
                              margin: const EdgeInsets.all(24),
                              maxLines: 2,
                              style: TextStyle(
                                  color: rgba(150, 151, 153, 1),
                                  fontSize: 16,
                                  fontWeight: FontWeight.bold),
                            )
                          ],
                        ),
                      )
                    : ListView.builder(
                        itemBuilder: (c, index) {
                          return GestureDetector(
                            behavior: HitTestBehavior.translucent,
                            onTap: () {
                              Get.to(PView(
                                pdfFile: list![index],
                              ));
                            },
                            onLongPress: () {
                              Get.to(const Selected(), arguments: list);
                            },
                            child: SizedBox(
                              height: 88,
                              child: Row(
                                children: [
                                  Container(
                                    margin: const EdgeInsets.only(right: 12),
                                    width: 56,
                                    height: 56,
                                    decoration: BoxDecoration(
                                        borderRadius: BorderRadius.circular(6),
                                        border: Border.all(
                                            color: rgba(230, 231, 235, 1),
                                            width: 0.5)),
                                    alignment: Alignment.center,
                                    child: PdfDocumentLoader.openFile(
                                      list![index].path,
                                      pageNumber: 1,
                                    ),
                                  ),
                                  Expanded(
                                    child: Column(
                                      children: [
                                        Expanded(
                                          child: Row(
                                            children: [
                                              Expanded(
                                                child: Column(
                                                  mainAxisAlignment:
                                                      MainAxisAlignment.center,
                                                  crossAxisAlignment:
                                                      CrossAxisAlignment.start,
                                                  children: [
                                                    TextView(
                                                      list[index].id.toString() +
                                                          list[index].name,
                                                      overflow: TextOverflow.clip,
                                                      maxLines: 2,
                                                      style: TextStyle(
                                                          color: ColorUtils.text,
                                                          fontSize: 14),
                                                    ),
                                                    Container(
                                                      margin:
                                                          const EdgeInsets.only(
                                                              top: 10),
                                                      child: Row(
                                                        crossAxisAlignment:
                                                            CrossAxisAlignment
                                                                .center,
                                                        children: [
                                                          TextView(
                                                              list[index].time +
                                                                  "   " +
                                                                  list[index]
                                                                      .size,
                                                              alignment: Alignment
                                                                  .center,
                                                              textAlign: TextAlign
                                                                  .center,
                                                              margin:
                                                                  const EdgeInsets
                                                                      .only(
                                                                      right: 10),
                                                              style: TextStyle(
                                                                  color:
                                                                      ColorUtils
                                                                          .text_,
                                                                  fontSize: 10)),
                                                          if (list[index].star)
                                                            const MyLocalImage(
                                                              Res.sys_icon_file_star,
                                                              width: 12,
                                                              height: 12,
                                                            )
                                                        ],
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                              MyLocalImage(
                                                Res.icon_file_more,
                                                width: 24,
                                                height: 24,
                                                margin: const EdgeInsets.all(6),
                                                onclick: () {
                                                  Get.bottomSheet(SetDialog(
                                                    pdfFile: list[index],
                                                  ));
                                                },
                                              ),
                                            ],
                                          ),
                                        ),
                                        const Divider(
                                          height: 1,
                                        )
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          );
                        },
                        itemCount: list.length,
                      ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
